package cn.csust.net2.manager.server.service.impl;

import java.util.List;
import java.util.Set;
import java.util.Map.Entry;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;

import cn.csust.net2.manager.client.module.ByGrade;
import cn.csust.net2.manager.client.ux.ExpandTree;
import cn.csust.net2.manager.server.dao.StudentDAO;
import cn.csust.net2.manager.shared.exception.BeyondAuthorityException;
import cn.csust.net2.manager.shared.po.Banji;
import cn.csust.net2.manager.shared.po.Major;
import cn.csust.net2.manager.shared.po.Student;
import cn.csust.net2.manager.shared.service.NoticeManagerService;
import cn.csust.net2.manager.shared.util.Constant;
import cn.csust.net2.manager.shared.util.ServiceConstant;
import cn.csust.net2.manager.shared.util.Utils;
import cn.csust.net2.manager.shared.vo.GridConfig;
import cn.csust.net2.manager.shared.vo.TreeNode;

@Service(value = ServiceConstant.SERVICE_NAME_NOTICEMANAGER)
public class NoticeManagerServiceImpl  extends TreeServiceImpl  implements  NoticeManagerService{


	@Resource(name = Constant.DAO_NAME_STUDENT)
	private StudentDAO studentDAO;
	@Override
	@Transactional
	public PagingLoadResult<Student> findAllByPaging(PagingLoadConfig config) throws BeyondAuthorityException{
		
		TreeNode cla = config.get(ExpandTree.TREE_NODE);
		GridConfig gc = config.get(ByGrade.GRID_FILTER);
		StringBuffer hql = new StringBuffer(" from Student as c where 1=1 ") ;
		if(cla!=null){
			if(cla.getSelf().equals(Major.class)){
				hql .append(" and  c.banji.major.majorID = " +cla.getNodeId());
			}else if(cla.getSelf().equals(Banji.class)){
				hql.append(" and  c.banji.banjiID = " +cla.getNodeId());
			}else if(cla.getSelf().equals(Student.class)){
				hql.append(" and  c.userID = " +cla.getNodeId());
			}else{
				hql.append(" and  c.banji.major.academy.academyID = " +cla.getNodeId());
			}
		}
		if(gc!=null){
			Set<Entry<String, Object>> ex = gc.entrySet();
			for(Entry<String, Object> entry:ex){
				String key = entry.getKey();
				String value = (String) entry.getValue();
				if(!value.equals(""))
					hql.append("  and c."+Utils.getOrg(key)+" like '%"+value+"%'");
			}
		}
		 return studentDAO.findAllByPaging(config,hql.toString());
	
	}
	
	@Transactional
	public  <G> List<G> hql(String hql) {
		// TODO Auto-generated method stub
		List<G> list = studentDAO.findListBySQL(hql);
		return list;
		
	}
	//reqestHTTP的指针，在远程方法寻找特定BREAN的时候初始化的
	protected HttpServletRequest request;
	//BaseService接口定义接收REQUEST对象的方法
	@Override
	public void loadRequest(HttpServletRequest request){
		this.request = request;
	}
	public StudentDAO getStudentDAO() {
		return studentDAO;
	}
	public void setStudentDAO(StudentDAO studentDAO) {
		this.studentDAO = studentDAO;
	}
}
